# -*- coding: utf-8 -*-

import scrapy
from ..items import MaoyanItem


class BangdanSpider(scrapy.Spider):
    name = 'bangdan'
    # allowed_domains = ['xxx']
    start_urls = ['https://maoyan.com/board/4?offset=0']
    for page in range(10, 100, 10):
        url = 'https://maoyan.com/board/4?offset=' + str(page)
        start_urls.append(url)

    def parse(self, response):
        all_href = response.xpath("//dl[@class='board-wrapper']/dd/div[@class='board-item-main']")
        # 12.	抓取100条（8分）
        for hh in all_href:
            pingfen = hh.xpath("string(.//p[@class='score'])")[0].extract()
            href = 'https://maoyan.com' + hh.xpath(".//p[@class='name']/a/@href")[0].extract()
            yield scrapy.Request(url=href, callback=self.parse_xq, meta={'pingfen': pingfen})

    def parse_xq(self, response):
        # 1.	获取电影名字（8分）
        name = response.xpath("//h3[@class='name']/text()")[0].extract()
        # 2.	上映地点（8分）
        didian = response.xpath("//li[@class='ellipsis'][2]/text()")[0].extract().split('/')[0].strip()
        # 3.	上映年份，时间（8分）
        shangying = response.xpath("//li[@class='ellipsis'][3]/text()")[0].extract()
        # 4.	导演名称（8分）
        daoyan = response.xpath("//li[@class='celebrity ']//a[@class='name']/text()")[0].extract().strip()
        # 5.	主演名称及嘉宾名称（8分）
        zhuyan = response.xpath("//li[@class='celebrity actor']//a[@class='name']/text()")[:4].extract()
        zhuyan = ','.join([i.strip() for i in zhuyan])
        # 6.	主演角色名称（8分）
        juese = response.xpath("//li[@class='celebrity actor']//span[@class='role']/text()")[:4].extract()
        juese = ','.join([i[2:] for i in juese])
        # 7.	评分（8分）
        pingfen = response.meta['pingfen']
        # 8.	剧情简介（8分）
        jianjie = response.xpath("//div[@class='mod-content']/span[@class='dra']/text()")[0].extract()
        # 9.	电影图片（8分）
        tupian = response.xpath("//div[@class='avatar-shadow']/img[@class='avatar']/@src")[0].extract()[:-16]
        # 10.	电影时长（8分）
        shichang = response.xpath("//li[@class='ellipsis'][2]/text()")[0].extract().split('/')[1].strip()
        # 11.	累积票房（8分）
        piaofang = response.xpath("string(//div[@class='movie-index-content box'])")[0].extract().strip()
        print(name)
        # 13.	将上述内容存储到mongo中（4分）
        item = MaoyanItem()
        item['name'] = name
        item['didian'] = didian
        item['shangying'] = shangying
        item['daoyan'] = daoyan
        item['zhuyan'] = zhuyan
        item['juese'] = juese
        item['pingfen'] = pingfen
        item['jianjie'] = jianjie
        item['tupian'] = tupian
        item['shichang'] = shichang
        item['piaofang'] = piaofang
        yield item
